import org.quartz.*;
import org.quartz.impl.StdSchedulerFactory;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Quartz
 */
public class doTest {
    public static void main(String[] args) throws SchedulerException, InterruptedException, ParseException {
        commonTask();
//        misTask_cron();
//        misTask_simple();
    }

    /**
     * 定时任务的一般执行方式
     */
    public static void commonTask() throws SchedulerException, InterruptedException, ParseException {
        //调度器
        SchedulerFactory factory = new StdSchedulerFactory();
        Scheduler scheduler = factory.getScheduler();
        scheduler.start();
        //任务
        JobDetail job = JobBuilder.newJob(MyJob.class).withIdentity("job01", "group01").build();
        //触发器
        Date startDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse("2021-10-14 22:14:00");
        Date endDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse("2021-10-14 21:42:00");
        Trigger trigger = TriggerBuilder.newTrigger()
                .withIdentity("trigger01", "Tgroup01")
                .startNow()
//                .startAt(startDate)//任务开始时间（超过此时间马上开始）
//                .endAt(endDate)//任务结束时间（超过此时间马上结束）
                //SimpleScheduleBuilder得SimpleTrigger；CronScheduleBuilder得CronTrigger
                //追加的执行次数，执行的间隔时间
                .withSchedule(SimpleScheduleBuilder.simpleSchedule().withRepeatCount(5).withIntervalInMilliseconds(5000))
                .build();
        //开始
        scheduler.scheduleJob(job, trigger);
        System.out.println("开始等待");
        for (int i=0; i<10; i++){
            Thread.sleep(10000);
            System.out.println((i+1)*10 + "秒");
        }
        scheduler.shutdown();
    }


    /**
     * 错过执行（cron）
     */
    public static void misTask_cron() throws SchedulerException, InterruptedException, ParseException {
        //调度器
        SchedulerFactory factory = new StdSchedulerFactory();
        Scheduler scheduler = factory.getScheduler();
        scheduler.start();
        //触发器
        Trigger trigger = TriggerBuilder.newTrigger()
                .withIdentity("trigger01", "Tgroup01")
                .startAt(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse("2021-10-18 14:43:00"))
                // 不设置则只执行一次（每隔15s执行一次）
//                .withSchedule(CronScheduleBuilder.cronSchedule("*/15 * * * * ?").withMisfireHandlingInstructionDoNothing())
//                .withSchedule(CronScheduleBuilder.cronSchedule("*/15 * * * * ?").withMisfireHandlingInstructionIgnoreMisfires())
                .withSchedule(CronScheduleBuilder.cronSchedule("*/15 * * * * ?").withMisfireHandlingInstructionFireAndProceed())
                .build();
        //任务
        JobDetail job = JobBuilder.newJob(MyJob.class).withIdentity("job01", "group01").build();

        scheduler.scheduleJob(job, trigger);

        for (int i=0; i<12; i++){
            Thread.sleep(10000);
            System.out.println((i+1)*10 + "秒");
        }
        scheduler.shutdown();
    }


    /**
     * 错过执行（simple）
     */
    public static void misTask_simple() throws SchedulerException, InterruptedException, ParseException {
        //调度器
        SchedulerFactory factory = new StdSchedulerFactory();
        Scheduler scheduler = factory.getScheduler();
        scheduler.start();
        //触发器
        Trigger trigger = TriggerBuilder.newTrigger()
                .withIdentity("trigger01", "Tgroup01")
                .startAt(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse("2021-10-18 16:30:00"))
//                .withSchedule(SimpleScheduleBuilder.simpleSchedule().withRepeatCount(100).withIntervalInSeconds(15).withMisfireHandlingInstructionFireNow())
//                .withSchedule(SimpleScheduleBuilder.simpleSchedule().withRepeatCount(100).withIntervalInSeconds(15).withMisfireHandlingInstructionIgnoreMisfires())
//                .withSchedule(SimpleScheduleBuilder.simpleSchedule().withRepeatCount(100).withIntervalInSeconds(15).withMisfireHandlingInstructionNowWithExistingCount())
//                .withSchedule(SimpleScheduleBuilder.simpleSchedule().withRepeatCount(100).withIntervalInSeconds(15).withMisfireHandlingInstructionNowWithRemainingCount())
//                .withSchedule(SimpleScheduleBuilder.simpleSchedule().withRepeatCount(100).withIntervalInSeconds(15).withMisfireHandlingInstructionNextWithExistingCount())
                .withSchedule(SimpleScheduleBuilder.simpleSchedule().withRepeatCount(100).withIntervalInSeconds(15).withMisfireHandlingInstructionNextWithRemainingCount())
                .build();
        //任务
        JobDetail job = JobBuilder.newJob(MyJob.class).withIdentity("job01", "group01").build();

        scheduler.scheduleJob(job, trigger);

        for (int i=0; i<12; i++){
            Thread.sleep(10000);
            System.out.println((i+1)*10 + "秒");
        }
        scheduler.shutdown();
    }
}
